package com.taiyi.crm.controller;

import com.taiyi.crm.pojo.entity.Product;
import com.taiyi.crm.service.IProductService;
import com.taiyi.crm.util.JSONResult;
import com.taiyi.crm.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @RequestMapping("getProductPage")
    public String getProductPage() {
        System.out.println("ProductController.getProductPage");
        return "/product/product_list";
    }

    @RequestMapping("getProductInsertPage")
    public String getProductInsertPage() {
        System.out.println("ProductController.getProductPage");
        return "/product/product_insert";
    }

    @RequestMapping("getProductUpdatePage")
    public String getProductUpdatePage() {
        System.out.println("ProductController.getProductUpdatePage");
        return "/product/product_update";
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public List<Product> selectAll() {
        System.out.println("ProductController.selectAll");
        List<Product> list = productService.selectAll();
        return list;
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayUITableResult list(Integer page, Integer limit, Product product, Date time) {
        System.out.println("ProductController.list");
        System.out.println(time);
        return productService.selectByPage(page, limit, product);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public JSONResult insert(Product product) {
        System.out.println("ProductController.insert()");
        productService.insert(product);
        return  JSONResult.ok();
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Integer id) {
        productService.deleteById(id);
        return JSONResult.ok("删除成功");
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(Product product) {
        productService.update(product);
        return JSONResult.ok("更新成功");
    }

}
